package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.Login;
import com.example.mall.dto.LoginDto;
import com.example.mall.mapper.LoginMapper;
import com.example.mall.query.LoginQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.service.ILoginService;
import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import com.example.mall.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户登录表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-14
 */
@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Login> implements ILoginService {

    @Autowired
    private LoginMapper loginMapper;

    @Override
    public PageDTO<LoginDto> queryPage(LoginQuery loginQuery) {
        if (loginQuery == null) {
            loginQuery = new LoginQuery();
        }
        Page<Login> page = loginQuery.toMpPageByDefaultCreateTimeAsc();
        Page<Login> cartPage = lambdaQuery()
                .eq(StringUtils.isNotEmpty(loginQuery.getAvatar()),Login::getAvatar,loginQuery.getAvatar())
                .like(StringUtils.isNotEmpty(loginQuery.getClientId()),Login::getClientId,loginQuery.getClientId())
                .like(StringUtils.isNotEmpty(loginQuery.getPassword()),Login::getPassword,loginQuery.getPassword())
                .eq(StringUtils.isNotEmpty(loginQuery.getClientSecret()),Login::getClientSecret,loginQuery.getClientSecret())
                .like(StringUtils.isNotEmpty(loginQuery.getUsername()),Login::getUsername,loginQuery.getUsername())
                .eq(loginQuery.getType() != null, Login::getType, loginQuery.getType())
                .eq(loginQuery.getEnabled() != null, Login::getEnabled, loginQuery.getEnabled())
                .eq(loginQuery.getAccountNonExpired() != null, Login::getAccountNonExpired, loginQuery.getAccountNonExpired())
                .eq(loginQuery.getAccountNonLocked() != null, Login::getAccountNonLocked, loginQuery.getAccountNonLocked())
                .eq(loginQuery.getCredentialsNonExpired() != null, Login::getCredentialsNonExpired, loginQuery.getCredentialsNonExpired())
                .page(page);
        return PageDTO.of(cartPage, LoginDto.class);
    }

    @Override
    public Login queryLoginById(Long id) {
        return loginMapper.selectById(id);
    }

    @Override
    public List<Login> queryLoginList() {
        // 方式1：直接使用Service的list方法（推荐）
        return this.list();
        // 方式2：通过Mapper查询
        // return loginMapper.selectList(null);
    }

    @Override
    public void insert(LoginDto loginDto) {
        // 新增购物车
        Login login = new Login();
        CopyUtils.copyProperties(loginDto, login);
        login.setId(CodeGenerateUtils.generateUniqueId());
        loginMapper.insert(login);
    }

    @Override
    public void update(LoginDto loginDto) {
        AssertUtil.isNotNull(loginDto.getId(),"用户登录信息ID不能为空");
        Login login = loginMapper.selectById(loginDto.getId());
        AssertUtil.isNotNull(login,"用户登录信息不存在");
        CopyUtils.copyProperties(loginDto, login);
        loginMapper.updateById(login);
    }

    @Override
    public void delete(Long id) {
        Login login = loginMapper.selectById(id);
        AssertUtil.isNotNull(login,"用户登录信息不存在");
        login.setEnabled(BaseConstants.LOSE_STATUS);
        loginMapper.updateById(login);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        List<Login> logins = loginMapper.selectBatchIds(ids);
        AssertUtil.isNotNull(logins,"用户登录信息不存在");
        logins.forEach(login -> {
            login.setEnabled(BaseConstants.LOSE_STATUS);
            loginMapper.updateById(login);
        });
    }
}
